-- Princess
-- Created by chengb Jul/26/2016
-- 公主格子

local PRINCESS_PET_ID = 69;     -- 公主对应的冈布奥编号

-- 初始化
function initPrincess(self)
    self.item:removeAllChildren();

    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    local iconPath = getHeroIconPath(PRINCESS_PET_ID);

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 有监狱图标，需要击杀所有怪物
            if self.gridData.need_prison == 1 then
                if not DungeonM.isAllMonsterDead() then
                    -- 还有怪没死
                    alert(getLocStr("swamp_cave_pick_tip"));
                    return;
                end

                local function okFunc()
                    -- 解救公主
                    if DungeonActionM.go("rescue_princess", self.gridData:getPos()) then
                        alert(getLocStr("rescue_princess_succ"));

                        -- 建筑消失
                        playBuildingDisappear(self);
                    end
                end
                self:openBuildingView(okFunc, nil, nil, nil, nil, iconPath);

                return;
            end

            -- 音效
            AudioM.playFx("ui_open1");

            if BraveVSDragonM.hasDefeatedDragon() then
                -- 已经击败龙王
                self:openBuildingView(nil, nil, buildingInfo["extra_desc"], buildingInfo["view_text2"], nil, iconPath);
            elseif BraveVSDragonM.hasGotBuildingBonus(self.gridData.class) then
                -- 已经收下公主的礼物
                self:openBuildingView(nil, nil, buildingInfo["dialog2"], buildingInfo["view_text2"], nil, iconPath);
            else
                -- 还没收下公主的礼物
                local function okFunc()
                    -- 收下公主的礼物
                    DungeonActionM.go("brave_building_bonus", self.gridData:getPos());
                end
                self:openBuildingView(okFunc, nil, nil, nil, nil, iconPath);
            end

        end
    end

    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);
    self.iconPath = iconPath;
    self.icon:setScale(1.2);

    -- 配置了监狱图标
    if self.gridData.need_prison == 1 then
        -- 需要用监狱图标遮挡
        local prisonIcon = getLevelItemIconPath("prison");
        local prisonImg  = ccui.ImageView:create(prisonIcon);
        prisonImg:setName("prison_icon");
        self.item:addChild(prisonImg);
    else
        self.item:setPositionY(-10);
    end

    -- 呼吸
    self.needAnimation = true;

    local isVisible;
    if BraveVSDragonM.isInSwampCave() then
        -- 沼泽洞窟中必定显示
        isVisible = true;
    else
        -- 公主未出现，格子隐藏
        isVisible = BraveVSDragonM.hasPrincessAppeared();
    end

    self.icon:setVisible(isVisible);
end

-- 公主出现的回调
function onPrincessAppeared(self)
    self.icon:setVisible(true);
    playSummonAppearEffect(self, 1178);
end